<?php
/**
 * @author Feng
 * @date 2012-5-28
 */
class controller_admin_picture_category extends system_user_controller_category {
		
	public function action_index() {
		$this->render();
	}
	
	public function action_special() {
		$row = $this->_getRow();
		$this->view->row = $row;
		$this->_crumbs()->addPage(system_nav_page::create($row->getLocale()->getTitle())->setRoute('picture/category/edit')->setParams(array('id' => $row->getId())));
		$this->_crumbs()->addPage(system_nav_page::create("Specials")->setRoute('picture/category/special')->setParams(array('id' => $row->getId())));

		$id = $row->getId();
		$render = null;
		$lang = $this->user->getLang();
		$this->_batchDelete(new picture_special_category_concept($lang), 'id', "lang = '$lang' AND category_id = '{$row->getId()}' AND special_id");
		if($this->getRequest()->isPost() && $this->_post('delete')) {
			try {
				$row->save();
			} catch (Exception $e) {
				
			}
		}
		if($this->getRequest()->isPost() && $this->_post('addSpecial')) {
			$specialList = array();
			$render = 'picture/category/addSpecial.phtml';
			$this->_crumbs()->addPage(system_nav_page::create($this->language('Add Special'))->setRoute('picture/category/special')->setParams(array('id' => $row->getId())));
			try {
				system_db::getAdapter()->beginTransaction();
				if($this->_post('addSpecialToCategory')) {
					$specialId =  (array) $this->_post('special_id');
					if(empty($specialId)) {
						throw new picture_exception("No Special selected.");
					}
					$_id = implode(',', $specialId);
					$concept = new picture_special_concept($this->user->getLang());
					$specialList = $concept->fetchAll(array("special.id IN($_id)"));
		
					$sql = "REPLACE INTO `picture_special_category`(`lang`, `special_id`, `category_id`) VALUES";
					$_sql = array();
					foreach($specialId AS $cid) {
						$_sql[] = "('$lang', '$cid', '$id')";
					}
					$sql .= implode(',', $_sql);
					system_db::getAdapter()->query($sql);
		
					$row->save();
					system_db::getAdapter()->commit();
					$this->view->throwMessage = 'Complete';
				}
			} catch (Exception $e) {
				system_db::getAdapter()->rollBack();
				$this->view->throwException = $e;
			}
			$this->view->specialList = $specialList;
		}
		if($render == null) {
			$this->_specialList($row);
		}
		$this->render($render);
	}
	
	public function action_delete() {
		$row = $this->_getRow();
		$localeList = array();
		$concept = new picture_category_locale_concept();
		foreach($concept->fetchAll(array('category_id = ?' => $row->getId())) AS $locale) {
			$localeList[$locale->getLang()] = $locale;
		}
		if($this->getRequest()->isPost()) {
			$this->referer = $this->_post('referer');
			try {
				system_db::getAdapter()->beginTransaction();
				$this->_auth();
				$row->delete($this->_post('options'));
				system_db::getAdapter()->commit();
				$this->view->throwMessage = 'Complete';
			} catch (Exception $e) {
				system_db::getAdapter()->rollBack();
				$this->view->throwException = $e;
			}
		}
		$this->view->localeList = $localeList;
		$this->view->referer = $this->referer;
		$this->view->row = $row;
		$this->_crumbs()->addPage(system_nav_page::create($row->getLocale()->getTitle())->setRoute('picture/category/edit')->setParams(array('id' => $row->getId())));
		$this->_crumbs()->addPage(system_nav_page::create("Delete")->setRoute('picture/category/delete')->setParams(array('id' => $row->getId())));
		$this->render();
	}
	
	public function action_addnew() {
		$row = $this->concept->create();
		$row->getLocale()->setParentId($this->pid);
		$row->getLocale()->setIsActive(1);
		$row->getLocale()->setDisplayOrder(0);
		$row = $this->_saveRow($row);
		$this->view->row = $row;
		$this->view->referer = $this->referer;
		$this->_navPage();
		$this->_crumbs()->addPage(system_nav_page::create($this->language('New Category'))->setRoute('picture/category/addnew')->setParams(array('parent' => $this->pid)));
		$this->render('picture/category/row.phtml');
	}
	
	public function action_edit() {
		$row = $this->_getRow();		
		$row = $this->_saveRow($row);
		$this->view->row = $row;
		$this->view->referer = $this->referer;
		$this->_navPage();
		$this->_crumbs()->addPage(system_nav_page::create($row->getLocale()->getTitle())->setRoute('picture/category/edit')->setParams(array('id' => $row->getId())));
		$this->render('picture/category/row.phtml');
	}
	
	public function action_move() {
		$row = $this->_getRow();
		if($this->getRequest()->isPost()) {
			$this->referer = $this->_post('referer');
			try {
				if($this->_post('top')) {
					$row->getLocale()->setParentId(0);
					$row->save();
				} else {
					if(!($to = intval($this->_post('to')))) {
						throw new picture_exception("Please select a category you want to move to.");
					}
					$row->getLocale()->setParentId($to);
					$row->save();
				}
				$this->view->throwMessage = "Complete";
			} catch (Exception $e) {
				$this->view->throwException = $e;
			}
		}
		$this->view->row = $row;
		$this->view->referer = $this->referer;
		$this->_navPage();
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Move To'))->setRoute('picture/category/move')->setParams(array('id' => $row->getId())));
		$this->render();
	}
	
	public function action_list() {
		$this->view->order = $order = strtolower($this->_get('order'));
		$_order = array("locale.is_active DESC", "locale.display_order ASC");
		if($order) {
			$this->view->sort = $sort = intval($this->_get('sort'));
			switch($order) {
				case 'date': {
					$order = 'lastmodified';
					break;
				}
				case 'special': {
					$order = 'locale.stotal';
					break;
				}
				default: {
					$sort = !$sort;
					$order = 'locale.display_order';
					break;
				}
			}
			$_sort = !$sort ? 'DESC' : 'ASC';
			$_order = array("$order $_sort");
		}

		$concept = new picture_category_concept($this->user->getLang());
		$list = $concept->fetchAll(array('locale.parent_id = ?' => $this->pid), $_order);
		$ids = $children = array();
		foreach($list AS $row) {
			$ids[] = $row->getId();
		}
		if(!empty($ids)) {
			$_ids = implode(', ', $ids);
			$sql = "SELECT a.`id`, (";
			$sql .= "SELECT COUNT(*) FROM `picture_category` AS `b` INNER JOIN `picture_category_locale` AS `l` ON `l`.`category_id` = `b`.`id` ";
			$sql .= "WHERE `l`.`parent_id` = `a`.`id` AND `l`.`lang` = '{$this->user->getLang()}'";
			$sql .= ") AS `count` FROM `picture_category` AS `a` WHERE `a`.`id` IN($_ids)";
			foreach(system_db::getAdapter()->query($sql) as $row) {
				$children[$row['id']] = $row['count'];
			}
		}
		if($this->getRequest()->isPost()) {
			try {
				$_dorder = (array) $this->_post('_display_order');
				$lang = $this->user->getLang();
				foreach((array)$this->_post('display_order') AS $id => $value) {
					if(isset($_dorder[$id]) && $_dorder[$id] == $value) continue;
					system_db::getAdapter()->query("UPDATE `picture_category_locale` SET `display_order` = '$value', `stotal` = (
							SELECT COUNT(1) FROM `picture_special_category` WHERE `category_id` = '$id' AND `lang` = '$lang'
						) WHERE `lang` = '$lang' AND `category_id` = '$id'");
				}
				$this->view->throwMessage = 'Complete';
			} catch (Exception $e) {
				$this->view->throwException = $e;
			}
		}

		$this->view->children = $children;
		$this->view->list = $list;
		$this->_navPage();
		$this->render();
	}

}
